import 'package:hive/hive.dart';
import 'dart:convert';

import '../generated/json/base/json_field.dart';
import '../generated/json/book_entity.g.dart';


@JsonSerializable()
@HiveType(typeId: 1)
class BookEntity extends HiveObject{

  @HiveField(0)
  String? no;

  @HiveField(1)
  String? place;

  @HiveField(2)
  @JSONField(name: "state_value")
  String? stateValue;

  @HiveField(3)
  String? isbn;

  @HiveField(4)
  @JSONField(name: "main_title")
  String? mainTitle;

  @HiveField(5)
  String? author;

  @JSONField(name: "search_no")
  @HiveField(6)
  String? searchNo;

  @HiveField(7)
  String? subject;

  @HiveField(8)
  String? publisher;

  @HiveField(9)
  @JSONField(name: "publish_date")
  String? publishDate;

  @HiveField(10)
  double? price;

  @HiveField(11)
  String? pages;


  //1 = 推荐
  //2 = 不推荐
  @HiveField(12)
  int? recommend = 2;

  //0在架
  //1外借
  @HiveField(13)
  int? statue = 0;

  @HiveField(14)
  String? cover;

  @HiveField(15)
  String? summary;



  //0 = 新增
  //1 = 在架
  //2 = 下架
  //-1 = 条码号错误
  //-2 = 图书未注册
  int stackStatus = 0;

  String? remark;


  BookEntity();

  factory BookEntity.fromJson(Map<String, dynamic> json) => $BookEntityFromJson(json);

  Map<String, dynamic> toJson() => $BookEntityToJson(this);

  @override
  String toString() {
    return jsonEncode(this);
  }
}

class BookEntityAdapter extends TypeAdapter<BookEntity> {
  @override
  final typeId = 1;

  @override
  BookEntity read(BinaryReader reader) {
    return BookEntity()
      ..no = reader.read()
      ..place = reader.read()
      ..stateValue = reader.read()
      ..isbn = reader.read()
      ..mainTitle = reader.read()
      ..author = reader.read()
      ..searchNo = reader.read()
      ..subject = reader.read()
      ..publisher = reader.read()
      ..publishDate = reader.read()
      ..price = reader.read()
      ..pages = reader.read()
      ..recommend = reader.read()
      ..cover = reader.read()
      ..summary = reader.read()
      ..statue = reader.read();
  }

  @override
  void write(BinaryWriter writer, BookEntity obj) {
    writer.write(obj.no);
    writer.write(obj.place);
    writer.write(obj.stateValue);
    writer.write(obj.isbn);
    writer.write(obj.mainTitle);
    writer.write(obj.author);
    writer.write(obj.searchNo);
    writer.write(obj.subject);
    writer.write(obj.publisher);
    writer.write(obj.publishDate);
    writer.write(obj.price);
    writer.write(obj.pages);
    writer.write(obj.recommend);
    writer.write(obj.cover);
    writer.write(obj.summary);
    writer.write(obj.statue);
  }
}



